package io.ticktick.modules.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.ticktick.modules.app.annotation.Login;
import io.ticktick.modules.app.entity.UserAccountEntity;
import io.ticktick.modules.app.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.ticktick.modules.app.entity.ChatMemberEntity;
import io.ticktick.modules.app.service.ChatMemberService;
import io.ticktick.common.utils.R;



/**
 * 群聊成员
 *
 * @author hwf
 * @email zhongku0225@gmail.com
 * @date 2019-11-13 17:15:19
 */
@RestController
@RequestMapping("app/chatmember")
public class ChatMemberController {
    @Autowired
    private ChatMemberService chatMemberService;
    @Autowired
    private UserAccountService userAccountService;
    /**
     * 群友列表信息
     */
    @Login
    @GetMapping("/memberlist/{groupId}")
    public R listMember(@PathVariable("groupId") Long groupId){

        List<ChatMemberEntity> list = chatMemberService.list(new QueryWrapper<ChatMemberEntity>().eq("group_id", groupId));

        for(ChatMemberEntity eac : list){
            eac.setUserAccountEntity(userAccountService.getOne(new QueryWrapper<UserAccountEntity>().eq("user_id", eac.getUserId())));
        }

        return R.ok().put("chatMembers", list);
    }

    /**
     * 群友列表
     */
    @Login
    @GetMapping("/memberaccount/{groupId}")
    public R listMemberAccount(@PathVariable("groupId") Long groupId){

        List<ChatMemberEntity> list = chatMemberService.list(new QueryWrapper<ChatMemberEntity>().eq("group_id", groupId));

        return R.ok().put("membersAccount", list);
    }

    /**
     * 群友列表
     */
//    @Login
//    @GetMapping("/list")
//    public R listGroup(@RequestParam("userId") Long userId){
//
//        List<UserAccountEntity> list = chatMemberService.list(new QueryWrapper<CharM>().eq("userId", userId));
//        return R.ok().put("chatMembers", list);
//    }


    /**
     * 信息
     */
    @Login
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
		ChatMemberEntity chatMember = chatMemberService.getById(userId);

        return R.ok().put("chatMember", chatMember);
    }

    /**
     * 保存
     */
    @Login
    @RequestMapping("/save")
    public R save(@RequestBody ChatMemberEntity chatMember){
		chatMemberService.save(chatMember);

        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @RequestMapping("/update")
    public R update(@RequestBody ChatMemberEntity chatMember){
		chatMemberService.updateById(chatMember);

        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
		chatMemberService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
